
const express = require("express");/*引入express*/
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.configure(function () {
    app.use(express.logger("dev"));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + "/src/public"));/*设置静态目录，当请求为/的时候，默认为public/index.html文件*/
    app.use(express.errorHandler());
})
var person = {};
var user=[];
var allUserNum=0;
io.on('connection', function (socket) {
    allUserNum++;/*客户端每次建立连接  在线人数+1*/
    socket.on('chatmessage', function (msg) {/*监听客户端发送的chatmessage事件*/
        person.name = msg.nickName;
        person.chatMsg = msg.Msg;
        person.AllPerson = allUserNum;
        var sendMsgTime = getDate();
        person.sendMsgTime = sendMsgTime;
        io.emit('message', person);/*向与服务器建立socket连接的客户端广播message事件*/
    });
    socket.on('allUser',function (msg) {
      io.emit('allUser',allUserNum)
    })
});
app.get('/searchAllPerson.do',function (req,resp) {
    resp.send(allUserNum)
})
app.get('/searchUserName.do',function (req,resp) {
    resp.send(user)
})
app.post('/userResigte.do',function (req,resp) {
    var name = req.body.username;
    user.push(name);
    resp.send('ok')
})

http.listen(8888, function () {
    console.log('listening on *:8888');
});

function getDate() {
    var Mydate = new Date();
    return Mydate.toLocaleString()
}